package cn.genmer.test.security.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 获取操作系统信息工具
 */
public class SystemUsageUtil {
    private static final long GB = 1024 * 1024 * 1024;
    private static final long MB = 1024 * 1024;

    private Runtime r = Runtime.getRuntime();
    Long startTime = null;
    Long startMemory = null;

    /**
     * 第一次记录一次内存， 第二次打印两次方法执行间隔消耗的内存，并重置CD
     */
    public void markingMemoryConsume(){
        if (startMemory == null){
            r.gc();
            startMemory = r.freeMemory();
        } else {
            System.out.println(String.format("消耗内存%sMB", (startMemory - r.freeMemory())/MB));
            startMemory = null;
        }
    }

    /**
     * 第一次记录一次内存， 第二次打印两次方法执行间隔消耗的内存，并重置CD
     */
    public void markingTimeConsume(){
        if (startTime == null){
            r.gc();
            startTime = System.currentTimeMillis();
        } else {
            System.out.println(String.format("消耗时间%sms", System.currentTimeMillis() - startTime));
            startTime = null;
        }
    }

    /**
     * 打印系统参数信息
     */
    public static void printSystemUsage() {
        while (true) {
            OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
            String osJson = JSON.toJSONString(operatingSystemMXBean);
//            System.out.println("osJson is " + osJson);
            JSONObject jsonObject = JSON.parseObject(osJson);
            double processCpuLoad = jsonObject.getDouble("processCpuLoad") * 100;
            double systemCpuLoad = jsonObject.getDouble("systemCpuLoad") * 100;
            Long totalPhysicalMemorySize = jsonObject.getLong("totalPhysicalMemorySize");
            Long freePhysicalMemorySize = jsonObject.getLong("freePhysicalMemorySize");
            double totalMemory = 1.0 * totalPhysicalMemorySize / GB;
            double freeMemory = 1.0 * freePhysicalMemorySize / GB;
            double memoryUseRatio = 1.0 * (totalPhysicalMemorySize - freePhysicalMemorySize) / totalPhysicalMemorySize * 100;

            StringBuilder result = new StringBuilder();
            result.append("系统CPU占用率: ")
                    .append(twoDecimal(systemCpuLoad))
                    .append("%，内存占用率：")
                    .append(twoDecimal(memoryUseRatio))
                    .append("%，系统总内存：")
                    .append(twoDecimal(totalMemory))
                    .append("GB，系统剩余内存：")
                    .append(twoDecimal(freeMemory))
                    .append("GB，该进程占用CPU：")
                    .append(twoDecimal(processCpuLoad))
                    .append("%");
            System.out.println(result.toString());

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static double twoDecimal(double doubleValue) {
        BigDecimal bigDecimal = new BigDecimal(doubleValue).setScale(2, RoundingMode.HALF_UP);
        return bigDecimal.doubleValue();
    }

}
